package com.chaoge.service.store.impl;

import com.chaoge.dao.store.CatalogDao;
import com.chaoge.domain.store.Catalog;
import com.chaoge.factory.MapperFactory;
import com.chaoge.service.store.CatalogService;
import com.chaoge.utils.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class CatalogServiceImpl implements CatalogService {
    @Override
    public void save(Catalog catalog) {
        SqlSession sqlSession=null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession,CatalogDao.class);
            //添加随机id
            String id = UUID.randomUUID().toString();
            catalog.setId(id);
            //添加时间
            catalog.setCreateTime(new Date());

            catalogDao.save(catalog);
            //提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public void delete(Catalog catalog) {
        SqlSession sqlSession=null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession,CatalogDao.class);
            catalogDao.delete(catalog);
            //提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Catalog catalog) {
        SqlSession sqlSession=null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession,CatalogDao.class);
            catalogDao.update(catalog);
            //提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public Catalog findById(String id) {
        SqlSession sqlSession=null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession,CatalogDao.class);
            return catalogDao.findById(id);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Catalog> findAll() {
        SqlSession sqlSession=null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession,CatalogDao.class);
            return catalogDao.findAll();
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {

        SqlSession sqlSession=null;
        try {
            //导入SqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取资源对象
            CatalogDao catalogDao = MapperFactory.getMapper(sqlSession,CatalogDao.class);
            PageHelper.startPage(page,size);
            List<Catalog> all = catalogDao.findAll();
            PageInfo pageInfo=new PageInfo(all);
            return pageInfo;
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
